/**************************************************************************************
 *  Copyright (c)  2010  Leszek Karcz.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 **************************************************************************************/
package view;
import java.awt.*;

import javax.swing.*;

import data.*;

/**************************************************************************************
 * Author(s): Leszek Karcz
 * This class displays the gameplay status: time/selected unit stats/player resources
 **************************************************************************************/
public class StatusView extends JPanel
{
	private static final long serialVersionUID = 7132605929747344358L;
	private GameplayPlayerData gameplayPlayerData;
	private Images images;
	private int w,h,x,y;

	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * StatusView constructor
	 **************************************************************************************/
	public StatusView(GameplayPlayerData gameplayPlayerData, Images images)
	{
		this.gameplayPlayerData=gameplayPlayerData;
		this.images=images;
		this.setFocusable(false);
		this.setOpaque(false);
		
		this.setLocation(0, 0);
		w = 1024;
		h = 600;
		x=0;
		y=0;
		
		this.setSize(w, h);
		this.setLocation(x, y);
	}

	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * paints/repaints the gameplay status
	 **************************************************************************************/
	public void paint(Graphics g)
	{
		this.setSize(w, h);
		this.setLocation(x, y);
		
		Font font = new Font("Arial", Font.BOLD, 12);
		g.setColor(Color.green);
		g.setFont(font);
		g.drawString("Options", 45, 17);
		
		if(!this.gameplayPlayerData.isClockRunning())
		{
			g.setColor(Color.red);
		}
		//print time
		g.drawString("Time: "+this.gameplayPlayerData.getTime(), 173, 17);
		
		g.setColor(Color.green);
		//print player resources
		int[] resources = this.gameplayPlayerData.getPlayerResources();
		g.drawString("Food: "+resources[0], 320, 17);
		g.drawString("Metal: "+resources[1], 440, 17);
		g.drawString("Energy: "+resources[2], 560, 17);
		
		//print player workers
		g.drawString("Workers: "+this.gameplayPlayerData.getWorkerCount(), 712, 17);
		
		int i=4,j=2;
		int posx,posy;
		posx=i*100-33+5;
		if((i % 2) != 0)
		{
			posy=j*116-58+30;
		}
		else
		{
			posy=j*116+30;
		}
		g.drawImage(images.getImage("cursor"), posx, posy, this);
		
		String[][] unitData=this.gameplayPlayerData.getTileUnitsData(i, j, true);
		
	
		for(int u=0;u<unitData.length;u++)
		{
			
			g.drawImage(images.getImage(unitData[u][0]), 25+(35*u), 562, this);
			g.drawImage(images.getImage(unitData[u][1]), 25+(35*u), 562, this);
			
			//draw health bar
			g.setColor(Color.BLACK);
			g.fillRect(26+(35*u), 593, 33, 4);
			g.setColor(Color.green);
			g.fillRect(27+(35*u), 594, (int)(31*Integer.parseInt(unitData[u][2])/100), 2);
		}
		
		g.setColor(Color.green);
		//draw selected controllable data
		String[] selectedType=this.gameplayPlayerData.getSelectedTypeData();
		
		//draw selected controllable icon
		g.drawImage(images.getImage(selectedType[0]), 845, 30, this);
		
		//print selected controllable type
		Font font2 = new Font("Arial", Font.BOLD, 18);
		g.setFont(font2);
		g.drawString(selectedType[1], 890, 54);
		g.setFont(font);
		
		//print selected controllable stats
		String[] selectedStats=this.gameplayPlayerData.getSelectedStatsData();
		for(int s=0;s<selectedStats.length;s++)
		{
			g.drawString(selectedStats[s], 850, 100+(s*14));
		}
		
		//print selected controllable commands
		String[] selectedCommands=this.gameplayPlayerData.getSelectedCommands();
		int selectedCommand=this.gameplayPlayerData.getSelectedCommand();
		for(int s=0;s<selectedCommands.length;s++)
		{
			if(s==selectedCommand)
			{
				g.drawRect(850, 280+(s*20), 145, 20);
				g.drawRect(851, 281+(s*20), 143, 18);
			}
			g.drawString(selectedCommands[s], 855, 295+(s*20));
		}
		
	}
}
